﻿using System;
using System.Collections.Generic;

namespace MessagePack.Formatters
{
	// Token: 0x02000023 RID: 35
	public class EnumAsStringFormatter<T> : IMessagePackFormatter<T>
	{
		// Token: 0x06000083 RID: 131 RVA: 0x00003098 File Offset: 0x00001498
		public EnumAsStringFormatter()
		{
			string[] names = Enum.GetNames(typeof(T));
			Array values = Enum.GetValues(typeof(T));
			this.nameValueMapping = new Dictionary<string, T>(names.Length);
			this.valueNameMapping = new Dictionary<T, string>(names.Length);
			for (int i = 0; i < names.Length; i++)
			{
				this.nameValueMapping[names[i]] = (T)((object)values.GetValue(i));
				this.valueNameMapping[(T)((object)values.GetValue(i))] = names[i];
			}
		}

		// Token: 0x06000084 RID: 132 RVA: 0x00003130 File Offset: 0x00001530
		public int Serialize(ref byte[] bytes, int offset, T value, IFormatterResolver formatterResolver)
		{
			string value2;
			if (!this.valueNameMapping.TryGetValue(value, out value2))
			{
				value2 = value.ToString();
			}
			return MessagePackBinary.WriteString(ref bytes, offset, value2);
		}

		// Token: 0x06000085 RID: 133 RVA: 0x00003168 File Offset: 0x00001568
		public T Deserialize(byte[] bytes, int offset, IFormatterResolver formatterResolver, out int readSize)
		{
			string text = MessagePackBinary.ReadString(bytes, offset, out readSize);
			T result;
			if (!this.nameValueMapping.TryGetValue(text, out result))
			{
				result = (T)((object)Enum.Parse(typeof(T), text));
			}
			return result;
		}

		// Token: 0x04000018 RID: 24
		private readonly Dictionary<string, T> nameValueMapping;

		// Token: 0x04000019 RID: 25
		private readonly Dictionary<T, string> valueNameMapping;
	}
}
